package com.pactera.madp.cp.service.sync.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.ftp.FtpConfig;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.cp.api.entity.sync.SyncCycleSetInfoEntity;
import com.pactera.madp.cp.common.constant.Constants;
import com.pactera.madp.cp.common.util.sync.SFTPUtils;
import com.pactera.madp.cp.common.util.sync.SyncUtils;
import com.pactera.madp.cp.config.PrdSftpConfig;
import com.pactera.madp.cp.config.SyncConfig;
import com.pactera.madp.cp.context.SyncStateContext;
import com.pactera.madp.cp.mapper.sync.SyncCycleSetInfoMapper;
import com.pactera.madp.cp.service.sync.SyncCycleSetInfoHisService;
import com.pactera.madp.cp.service.sync.SyncCycleSetInfoService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
@AllArgsConstructor
public class SyncCycleSetInfoServiceImpl extends ServiceImpl<SyncCycleSetInfoMapper, SyncCycleSetInfoEntity> implements SyncCycleSetInfoService {

    private final SyncCycleSetInfoHisService syncCycleSetInfoHisService;
    private final SyncConfig syncConfig;
    private final PrdSftpConfig prdSftpConfig;

    @Override
    public void addYesterdayToHis() {
        log.debug("产品周期方案：addYesterdayToHis");
        String date = DateUtil.format(new Date(), Constants.DATE_FORMAT);
        syncCycleSetInfoHisService.syncOneDay(date);
    }

    @Override
    public void deleteYesterday() {
        log.debug("产品周期方案：deleteYesterday");
        this.remove(Wrappers.emptyWrapper());
    }

    @Override
    public void parse() {
        log.debug("FTP获取数据文件");
        String fileDate = SyncStateContext.get().getFileDate();
        // 下载文件设置
        String srcFtpFile = SyncUtils.buildFilePath(syncConfig.getPrdFtpFile(), Constants.FN_CYCLE_SET, fileDate);
        String destFile = SyncUtils.buildFilePath(syncConfig.getPrdPath(), Constants.FN_CYCLE_SET, fileDate);
        // 下载目录检测
        String destDir = SyncUtils.buildFilePath(syncConfig.getPrdPath(), "", fileDate);
        boolean flag = SyncUtils.checkDir(destDir, true);
        if (!flag) {
            throw new RuntimeException("产品周期方案文件下载目录不存在");
        }
        // 文件下载
        FtpConfig ftpConfig = new FtpConfig();
        BeanUtil.copyProperties(prdSftpConfig, ftpConfig);
        flag = SFTPUtils.download(ftpConfig, srcFtpFile, destFile);
        if (!flag) {
            throw new RuntimeException("产品周期方案文件下载失败");
        }

        log.debug("产品周期方案：parse");
        File newestFile = SyncUtils.getNewestFile(destFile);
        if (ObjectUtil.isNull(newestFile)) {
            throw new RuntimeException("产品周期方案文件不存在");
        }
        List<SyncCycleSetInfoEntity> entityList
                = SyncUtils.parse(newestFile, Constants.SEPARATOR_VERTICAL_LINE, SyncCycleSetInfoEntity.class, syncConfig.getPrdCharset());

        specialHandle(entityList);

        this.saveBatch(entityList);
    }

    @Override
    public void deleteHisOneDay() {
        log.debug("产品周期方案：deleteHisOneDay");
        DateTime offsetDay = DateUtil.offsetDay(new Date(), -30);
        String date = DateUtil.format(offsetDay, Constants.DATE_FORMAT);
        syncCycleSetInfoHisService.deleteOneDate(date);
    }

    @Override
    public void syncBusiness() {
        log.debug("产品周期方案：syncBusiness");
        SyncStateContext.get().setFinalState(true);
    }

    // 行情信息主键设置
    private void specialHandle(List<SyncCycleSetInfoEntity> entityList) {
        // 初始化主键值为1
        int id = 1;
        // 遍历为每个从业资格对象设置主键
        for (SyncCycleSetInfoEntity e : entityList) {
            e.setId(id++);
        }
    }

}